package com.xinqizhi.controller;

import com.xinqizhi.entity.XqzSchool;
import com.xinqizhi.service.XqzSchoolService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**  
 * @ClassName: XqzSchoolController  
 * @Description: ${todo}(控制器)  
 * @author yangtongquan  
 * @date 2019-05-21  
 *  
 */
@Controller
@RequestMapping("/xqz")
public class XqzSchoolController   {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
	private XqzSchoolService schoolService;

	/**
	 * 跳转到学校管理的主页列表
	 * @return
	 */

	@RequestMapping("/school_index")
	public String toMenuListJsp(){
		return "redirect:../views/school.jsp";
	}

	/**
	 * 添加一所分校
	 * @param schoolStaffId 负责人工号
	 * @param schoolAddr 学校地址
	 * @return
	 */
	@RequestMapping("/add_school")
	@ResponseBody
	public Map addASchool(Integer schoolStaffId,String schoolAddr){
		logger.info(schoolAddr+"==================="+schoolStaffId);
		HashMap<String, Object> result = new HashMap<>(10);
		if (schoolAddr ==null||schoolStaffId==null){
			result.put("success",false);
			result.put("msg","请求数据不正确");
			return result;
		}
		//先校验工号是否存在
		//pass
		//pass
        Boolean aBoolean = schoolService.addASchool(schoolStaffId, schoolAddr);
        try {
            if (aBoolean){
                result.put("success",true);
                result.put("msg","添加分校成功");
            }else {
                result.put("success",false);
                result.put("msg","添加失败");
            }
        }catch (Exception e){
            result.put("success",false);
            result.put("msg","添加失败"+StringUtils.substring(e.getMessage(),0,100));
        }
		return result;
	}

    /**
     * 获取所有学校列表
     * @return
     */
	@RequestMapping("/school_list")
    @ResponseBody
    public Map getSchoolList(){
        HashMap<String, Object> result = new HashMap<>(5);
       List<XqzSchool> xqzSchools= schoolService.getSchoolList();
       result.put("data",xqzSchools);
       result.put("success",true);
       result.put("msg","查询分校信息成功");
        return result;
    }

    /**
     * 根据ID查询学校信息
     */
    @RequestMapping("/school")
    @ResponseBody
    public Map getScoolById(Integer schId){
        logger.info("---------------schoold="+schId);
        HashMap<String, Object> result = new HashMap<>(5);
        if (schId==null) {
            result.put("success",false);
            result.put("msg","参数不合法");
        }else {
          XqzSchool school= schoolService.getSchoolById(schId);
          result.put("data",school);
          result.put("success",true);
          result.put("msg","查询数据成功");
        }
        return result;
    }

    /**
     * 修改学校信息
     */
    @RequestMapping("/update_school")
    @ResponseBody
    public Map updateSchoolById(XqzSchool school)
    {
        logger.info("=====获取参数"+school.toString());
        HashMap<String, Object> result = new HashMap<>();
        Integer f = schoolService.updateSchool(school);
        if (f == null) {
            result.put("success",false);
            result.put("msg","修改失败");
        }else {
            result.put("success",true);
            result.put("msg","修改成功");
        }
        return result;
    }

    /**
     * @删除分校
     */
    @RequestMapping("/del_school")
    @ResponseBody
    public Map del_school(Integer schId)
    {
        HashMap<String, Object> result = new HashMap<>();
        if (schId == null) {
            result.put("success",false);
            result.put("msg","参数不合法");
            return result;
        }
        Integer re=schoolService.del_School(schId);
            result.put("success",true);
            result.put("msg","删除成功");
        return result;
    }
}
